<?php

/**
 * Roles View
 * @author 		TAMDV
 * @date		2008 07 22 (yyyy MM dd)
 * @package    	Joomus.JBS
 * @subpackage 	Components
 * @link 		http://localhost/jbschedule/administrator/index.php?option=com_jbschedule
 * @license		GNU/GPL
 */

// no direct access
//defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

/**
 * Roles View
 *
 * @package    Joomus.JBS
 * @subpackage Components
 */
class JBSViewRoles extends JView {

	function display($tpl = null) {
		$task = JRequest :: getVar('task');

		switch ($task) {
			case 'edit':
			case 'add' :
				$modelRoles = $this->getModel('roles');
				//get the functions
				$functions = & $this->get('Functions');

				//get the roles
				$roles = & $this->get('Data');
				$isNew = ($roles->id < 1);

				$text = $isNew ? JText :: _('New') : JText :: _('Edit');
				JToolBarHelper :: title(JText :: _('PROJECT NAME') . ' - ' . JText :: _('Roles') . ': <small><small>[ ' . $text . ' ]</small></small>', 'jbschedule');
				JToolBarHelper :: save();
				if ($isNew) {
					JToolBarHelper :: cancel();
				} else {
					// for existing items the button is renamed `close`
					JToolBarHelper :: cancel('cancel', 'Close');
				}

				//Get Function by role id
				$fids = $modelRoles->getFunctionsByRoleId($roles->id);

				//Input checkbox Item permission
				$inputCheckBox = "";
				foreach ($functions as $f) {
					$checked = $this->checkItemOfArray($f->id, $fids) ? "checked='checked'" : "";
					$inputCheckBox .= "<input type=\"checkbox\"" . $checked . " name=\"fid[]\" value=\"" . $f->id . "\"/>" . $f->description . "<br>";
				}

				//Get group function
				$groupFunction = $modelRoles->getGroupFunctions();
				$html = "";
				foreach ($groupFunction as $gf) {
					//$html.="<td width=\"30px\"> \n";
					$html .= "<div style=\"width:200px; float:left; padding:10px\">\n";
					$html .= "<fieldset class=\"adminform\"> \n";
					$html .= "<legend>" . JText::_($gf->name) . "</legend>\n";
					$functionByGroup = $modelRoles->getFunctionsByGroupId($gf->id);
					$html .= "<label for=\"allbox" . $gf->id . "\">\n";
					$html .= "<input type=\"checkbox\" id=\"allbox" . $gf->id . "\" name=\"allbox" . $gf->id . "\" onclick=\"return CheckAll(" . $gf->id . ")\"/>".JText::_('SELECT ALL')."<hr>\n";
					$html .= "</label>\n";
					foreach ($functionByGroup as $fid) {
						$checked = $this->checkItemOfArray($fid->id, $fids) ? "checked='checked'" : "";
						$html .= "<label for=\"" . $gf->id . "_chkFunction_" . $fid->id . "\"><input type=\"checkbox\"" . $checked . " id=\"" . $gf->id . "_chkFunction_" . $fid->id . "\" name=\"fid[]\" value=\"" . $fid->id . "\"/>" . JText::_($fid->description) . "</label><br>";
					}
					$html .= "</fieldset> \n";
					$html .= "</div> \n";
				}

				//In Role
				$htmlRole = "<table border=\"0\"> \n";
				$htmlRole .= "<tr> \n";
				$htmlRole .= "<td> \n";
				$htmlRole .= "<select size=\"6\" id=\"tableOne\" name=\"tableOne[]\" multiple style=\"width: 200px\">";
				//echo $roles->id;
				$userIsRole = $modelRoles->getUserIsRole($roles->id);
				if (count($userIsRole) > 0) {
					foreach ($userIsRole as $uir) {
						$htmlRole .= "<option value=\"" . $uir->id . "\">" . $uir->username . "</option>";
					}
				}
				$htmlRole .= "</select>";
				$htmlRole .= "<td valign=\"top\">";
				$htmlRole .= "<input type=\"button\" style=\"width:100px\" value=\" " . JText :: _("REMOVE") . " &rarr;\" onClick=\"domSwap(document.getElementById('tableOne'), document.getElementById('tableTwo'));\"/><br> <br>";
				$htmlRole .= "<input type=\"button\" style=\"width:100px\" value=\" &larr; " . JText :: _("ADD") . "\" onClick=\"domSwap(document.getElementById('tableTwo'), document.getElementById('tableOne'))\"/><br>";
				$htmlRole .= "</td>";

				$htmlRole .= "<td> \n";
				$htmlRole .= "<select size=\"6\" id=\"tableTwo\" name=\"tableTwo\" multiple style=\"width: 200px\">";
				$userIsNotRole = $modelRoles->getUserIsNotRole();
				foreach ($userIsNotRole as $uinr) {
					$htmlRole .= "<option value=\"" . $uinr->id . "\">" . $uinr->username . "</option>";
				}
				$htmlRole .= "</select>";
				$htmlRole .= "</td> \n";
				$htmlRole .= "</tr> \n";
				$htmlRole .= "</table> \n";

				//assign template
				$this->assignRef('roles', $roles);
				$this->assignRef('functions', $functions);
				$this->assignRef('inputCheckBox', $inputCheckBox);
				$this->assignRef('html', $html);
				$this->assignRef('htmlRole', $htmlRole);

				break;

			default :

				global $mainframe, $option;
				//initialise variables
				//$document	= & JFactory::getDocument();
				$db			= & JFactory::getDBO();
				//$user 	= & JFactory::getUser();



				//get vars
				$filter_order		= $mainframe->getUserStateFromRequest( $option.'.roles.filter_order', 'filter_order', 	'name', 'cmd' );
				$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.roles.filter_order_Dir', 'filter_order_Dir', '', 'word' );
				$search 			= $mainframe->getUserStateFromRequest( $option.'.roles.search', 'search', '', 'string' );
				$search 			= $db->getEscaped( trim(JString::strtolower( $search ) ) );
				$template			= $mainframe->getTemplate();



				JToolBarHelper :: title(JText :: _('PROJECT NAME') . ' - ' . JText :: _('ROLES MANAGER'), 'jbschedule');
				JToolBarHelper :: deleteList(JText :: _('ARE YOU SURE')); //param: 1: message 2: task to do 3: Label of the button
				JToolBarHelper :: editListX();
				JToolBarHelper :: addNewX();

				// Get data from the model
				$items      	= & $this->get( 'DataList');
				//$total      = & $this->get( 'Total');
				$pageNav 	= & $this->get( 'Pagination' );

				// table ordering
				$lists['order_Dir'] = $filter_order_Dir;
				$lists['order'] = $filter_order;

				// search filter
				$lists['search']= $search;

				$this->assignRef('items'		, $items);
				$this->assignRef('lists'      	, $lists);
				$this->assignRef('pageNav' 		, $pageNav);
				$this->assignRef('template'		, $template);

		}

		parent :: display($tpl);
	}

	//Check item on Array Item ?
	public function checkItemOfArray($item, $arr) {
		foreach ($arr as $ar) {
			if ($ar->function_id == $item)
				return true;
		}
		return false;
	}
}